home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / miscnix.zip / SPLIT.C < prev    next >
Text File  |  1988-05-28  |  2KB  |  108 lines

  1. #include <stdio.h>
  2.  
  3. /**    SPLIT    Print last several lines of a file.
  4.  
  5.     Usage:    split [-n] [ifile [ofile]]
  6.         where n is the number of lines per piece.
  7.  
  8.         If ifile is given as "-", or if no files are given and standard
  9.         input is a pipe or file, then input will come from standard
  10.         input.  Output file names are assigned sequentially:  (ofile)aa,
  11.         (ofile)ab, etc.  The default number of lines is 1000; the
  12.         default output file name is "x" (i.e., xaa, xab, etc.).
  13.  
  14.     No copyright, dammit:  this is PUBLIC DOMAIN!!!
  15. */
  16.  
  17. int    nlines    =1000;
  18. int    f1    =0;    /* file descriptor for input */
  19.  
  20. int    f2;        /* file descriptor for output */
  21. char    f2name[128];
  22. int    f2count;
  23. char    *f2ptr;        /* pointer to where to put the 'aa', etc. */
  24.  
  25. #define    BUFLEN    31*1024
  26. char    buf[BUFLEN];
  27.  
  28. usage() {
  29.     puts("Usage:    split [-n] [ifile [ofile]]");
  30.     puts("    where n is the number of lines per piece (default 1000).");
  31.     exit(1);
  32. }
  33.  
  34. errquit(s1,s2)
  35.     char *s1,*s2; {
  36.     fputs(s1,stderr);
  37.     fputs(":  ",stderr);
  38.     fputs(s2,stderr);
  39.     fputs("\n",stderr);
  40.     exit(2);
  41. }
  42.  
  43. opennext() {
  44.     *f2ptr = 'a'+(f2count/26);
  45.     *(f2ptr+1) = 'a'+(f2count%26);
  46.     ++f2count;
  47.     f2=creat(f2name);
  48.     if (f2==ERR) errquit(f2name,"cannot open output file.");
  49. }
  50.  
  51. processfile(s)
  52.     char *s; {    /* file name */
  53.     int    lleft;
  54.     char    *bufbgn,*bufpos,*bufend;
  55.     int    len;
  56.  
  57.     strcpy(f2name,s);
  58.     f2ptr= &f2name+strlen(f2name);
  59.     *(f2ptr+2)='\0';
  60.  
  61.     opennext();
  62.     lleft=nlines;
  63.     for (;;) {    /* loop over buffers read */
  64.         if ((len=read(f1,buf,BUFLEN))==0) break;
  65.         bufbgn=bufpos= &buf;
  66.         bufend= &buf+len;
  67.         for (;;) {    /* loop over lines or fragments thereof */
  68.         bufpos=strnchr(bufpos,bufend-bufpos,'\n');
  69.         if (bufpos>=bufend) break;
  70.         ++bufpos;
  71.         if ((--lleft) == 0) {
  72.             write(f2,bufbgn,bufpos-bufbgn);
  73.             close(f2);
  74.             bufbgn=bufpos;
  75.             opennext();
  76.             lleft=nlines;
  77.         }
  78.         }
  79.         write(f2,bufbgn,bufend-bufbgn);
  80.     }
  81.     close(f2);
  82. }
  83.  
  84. main(argc,argv)
  85.     int argc;
  86.     char **argv; {
  87.     int    arg    =1;
  88.  
  89.     /* parse numeric argument */
  90.  
  91.     if (argc>1 && argv[1][0]=='-' && argv[1][1]) {
  92.         if ((nlines=atoi(&argv[1][1]))==0) usage();
  93.         ++arg;
  94.     }
  95.  
  96.     if (arg >= argc) {
  97.         if (isatty(0)) usage();
  98.         argv[--arg]="-";
  99.     }
  100.     if (arg<argc-2) usage();
  101.  
  102.     /* f1=0; /* standard input */
  103.     if (strcmp(argv[arg],"-"))
  104.         if ((f1=open(argv[arg],0))==ERR)
  105.         errquit(argv[arg],"cannot open input file");
  106.     processfile(arg<argc-1? argv[arg+1]: "x");
  107. }
  108.